package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


import serverdata.ServerDataRecord;
import serverdata.cache_Manager;

public class query_authenticate extends query {
	public query_authenticate(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}
	//
	public boolean sync_authen(int user_id, String imsi, String imei)
	{	
		Connection con = null;
		try
		{
			boolean bret = true; 
			ServerDataRecord.userAuthenticateRecord uar = new ServerDataRecord.userAuthenticateRecord(imsi,imei);
			if(!cache_Manager.get_userCache().checkEqual(user_id, uar))
			{			
				con = this.init();		
				boolean needcheckvisitor = true;
				bret = this.pass_user(con,user_id, imsi, imei, needcheckvisitor);
				if(!bret)
				{
					if(needcheckvisitor)
						bret = this.pass_visitor(con,user_id, imsi, imei);
				}
				
				if(bret)
				{
					cache_Manager.get_userCache().putValue(user_id, uar);					
				}
			}
			return bret;			
		}		
		catch(Exception e)
		{
		     System.out.println("sync_authen exception: "+e.getMessage());
			e.printStackTrace();
			return false;
		}
		finally
		{
			this.release_connection(con);
			this.finalize();
		}
	}
	
	
	private boolean pass_user(Connection con,int user_id, String imsi, String imei, boolean needcheckvisitor)
	{
		querydb qdb = null;
		ResultSet rs = null;
		try
		{
			boolean bret = false;
			needcheckvisitor = true;			
			qdb = new querydb(con, sqlscripts.sql_authen_user_select);
			bret = qdb.prePS();
			if(bret)
			{
				qdb.ps.setInt(1, user_id);
				rs = qdb.execPSQuery();
				if(!rs.next())
				{
					bret = false;
					needcheckvisitor = true;
				}
				else
				{
					String dimsi = rs.getString(sqlscripts.sql_authen_user_select_c1_imsi);
					String dimei = rs.getString(sqlscripts.sql_authen_user_select_c2_imei);
					
					if(imsi.equals(dimsi) && imei.equals(dimei))
					{
						bret = true; //pass
					}
					else
						bret = false; //fail
				}
			}
			else
			{
				bret = false;
				needcheckvisitor = true; 
			}			
			
			return bret;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return false;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
		}
		
	}
	private boolean pass_visitor(Connection con,int visitor_id, String imsi, String imei)
	{
		querydb qdb = null;
		ResultSet rs = null;
		try
		{
			boolean bret = false;			
			
			qdb = new querydb(con, sqlscripts.sql_authen_user_select2);
			bret = qdb.prePS();
			if(bret)
			{
				qdb.ps.setInt(1, visitor_id);
				rs = qdb.execPSQuery();
				if(!rs.next())
				{
					bret = false;					
				}
				else
				{
					String dimsi = rs.getString(sqlscripts.sql_authen_user_select_c1_imsi);
					String dimei = rs.getString(sqlscripts.sql_authen_user_select_c2_imei);
					
					if(imsi.equals(dimsi) && imei.equals(dimei))
					{
						bret = true; //pass
					}
					else
						bret = false; //fail
				}
			}
			else
			{
				bret = false;				 
			}
			
			return bret;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return false;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
		}
	}

}
